//
//  AXFMyMessageViewController.m
//  AIXF
//
//  Created by Macx on 2017/7/14.
//  Copyright © 2017年 Macx. All rights reserved.
//

#import "AXFMyMessageViewController.h"
#import "AXFSystemMessageViewModel.h"
#import "AXSystemMessageTableViewCell.h"
#import "AXFMineSystemMessage.h"
#import "AXFMessageHeadView.h"
@interface AXFMyMessageViewController ()<UITableViewDataSource>

@property (nonatomic,strong)NSArray <AXFMineSystemMessage *>* dataArray;

@property (nonatomic,strong)UITableView *tableView;

@property (nonatomic,strong)AXFMessageHeadView * headView;

@end

@implementation AXFMyMessageViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.dataArray = [AXFSystemMessageViewModel shared].modelArray;
    
    self.navigationController.navigationBar.translucent = NO;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveModelArray) name:@"postAXFMineSystemMessage"  object:nil];
    NSArray *arr = @[@"1",@"2"];
    
    UISegmentedControl *segmentedControl = [[UISegmentedControl alloc]initWithItems:arr];
    
    segmentedControl.selectedSegmentIndex = 0;
    
    self.navigationItem.titleView = segmentedControl;
    
    segmentedControl.tintColor = [UIColor colorWithRed:1 green:0.8 blue:0.23 alpha:1];
    
    [segmentedControl setTitle:@"系统消息" forSegmentAtIndex:0];
    
    [segmentedControl setTitle:@"我的消息" forSegmentAtIndex:1];
    
    [segmentedControl addTarget:self action:@selector(didClicksegmentedControlAction:)forControlEvents:UIControlEventValueChanged];
    
    
    [self setupView];
    
    if (self.dataArray.count > 0)
    {
        [self setupUI];
    }
 
}

- (void)setupUI
{
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:tableView];
    tableView.dataSource = self;
    self.tableView = tableView;
    [tableView registerNib:[UINib nibWithNibName:@"AXSystemMessageTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    tableView.rowHeight = 180;
    
   
}

- (void)receiveModelArray
{
    self.dataArray = [AXFSystemMessageViewModel shared].modelArray;
    
    [self setupUI];
    
    [self.tableView reloadData];
    
}

- (void)setupView
{
    AXFMessageHeadView *headView= [[AXFMessageHeadView alloc] initWithFrame:self.view.bounds];
    
    
    _headView = headView;
    
}

- (void)didClicksegmentedControlAction:(UISegmentedControl *)segmentCtr
{
    
    
    if (segmentCtr.selectedSegmentIndex == 0)
    {
        self.dataArray = [AXFSystemMessageViewModel shared].modelArray;
        
        [self setupUI];
        
        [self.tableView reloadData];
        
    }else
    {
        self.tableView.tableHeaderView = self.headView;
        
        self.dataArray = nil;
        
        self.tableView.tableFooterView = [UIView new];
        
        [self.tableView reloadData];
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    AXSystemMessageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.model = self.dataArray[indexPath.row];
    return cell;
}


@end
